{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "nbsphinx": "hidden"
   },
   "source": [
    "This notebook is part of the `clifford` documentation: https://clifford.readthedocs.io/."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example 1 Interpolating Conformal Objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we will look at a few of the tools provided by the clifford package for (4,1) conformal geometric algebra (CGA) and see how we can use them in a practical setting to interpolate geometric primitives.\n",
    "\n",
    "The first step in using the package for CGA is to generate and import the algebra:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clifford.g3c import *\n",
    "blades"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This creates an algebra with the required signature and imports the basis blades into the current workspace. We can check our metric by squaring our grade 1 multivectors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('e1*e1 ', e1*e1)\n",
    "print('e2*e2 ', e2*e2)\n",
    "print('e3*e3 ', e3*e3)\n",
    "print('e4*e4 ', e4*e4)\n",
    "print('e5*e5 ', e5*e5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As expected this gives us 4 basis vectors that square to 1.0 and one that squares to -1.0, therefore confirming our metric is (4,1).\n",
    "\n",
    "The up() function implements the mapping of vectors from standard 3D space to conformal space. We can use this to construct conformal objects to play around with. \n",
    "\n",
    "For example a line at the origin:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_a = ( up(0)^up(e1+e2)^einf ).normal() \n",
    "print(line_a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The tools submodule of the clifford package contains a wide array of algorithms and tools that can be useful for manipulating objects in CGA. We will use these tools to generate rotors that rotate and translate objects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clifford.tools.g3 import *\n",
    "from clifford.tools.g3c import *\n",
    "from numpy import pi\n",
    "\n",
    "rotation_radians = pi/4\n",
    "euc_vector_in_plane_m = e1\n",
    "euc_vector_in_plane_n = e3\n",
    "\n",
    "euc_translation = -5.2*e1 + 3*e2 - pi*e3\n",
    "\n",
    "rotor_rotation = generate_rotation_rotor(rotation_radians, euc_vector_in_plane_m, euc_vector_in_plane_n)\n",
    "rotor_translation = generate_translation_rotor(euc_translation)\n",
    "print(rotor_rotation)\n",
    "print(rotor_translation)\n",
    "\n",
    "combined_rotor = (rotor_translation*rotor_rotation).normal()\n",
    "\n",
    "line_b = (combined_rotor*line_a*~combined_rotor).normal()\n",
    "print(line_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the above snippet of code we have generated rotors for translation and rotation, then combined these, then applied the combined rotor to the original line that we made.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizations\n",
    "\n",
    "The `clifford` package can be used alongside `pyganja` to render CGA objects which can be rotated interactively in a jupyter notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from pyganja import GanjaScene, draw\n",
    "sc = GanjaScene()\n",
    "sc.add_object(line_a,color=0xFF0000, label='a')\n",
    "sc.add_object(line_b,color=0x00FF00, label='b')\n",
    "draw(sc, scale=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also interpolate the objects using the tools in clifford and can visualise the result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def interpolate_objects_linearly(L1, L2, n_steps=10, color_1=np.array([255,0,0]), color_2=np.array([0,255,0])):\n",
    "    alpha_list = np.linspace(0, 1, num=n_steps)\n",
    "    intermediary_list = []\n",
    "    sc = GanjaScene()\n",
    "    for alpha in alpha_list:\n",
    "        intermediate_color = (alpha*color_1 + (1-alpha)*color_2).astype(np.uint8)\n",
    "        intermediate_object = interp_objects_root(L1, L2, alpha)\n",
    "        intermediary_list.append(intermediate_object)\n",
    "        color_string = int(\n",
    "            (intermediate_color[0] << 16) | (intermediate_color[1] << 8) | intermediate_color[2]\n",
    "        )\n",
    "        sc.add_object(intermediate_object, color_string)\n",
    "    return intermediary_list, sc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "intermediary_list, finished_scene = interpolate_objects_linearly(line_a, line_b)\n",
    "draw(finished_scene, scale=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can do the same for all the other geometric primitives as well"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Circles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "circle_a = (up(0)^up(e1)^up(e2)).normal()\n",
    "circle_b = (combined_rotor*circle_a*~combined_rotor).normal()\n",
    "intermediary_list, finished_scene = interpolate_objects_linearly(circle_a, circle_b)\n",
    "draw(finished_scene, scale=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Point pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_pair_a = (up(e3)^up(e1+e2)).normal()\n",
    "point_pair_b = (combined_rotor*point_pair_a*~combined_rotor).normal()\n",
    "intermediary_list, finished_scene = interpolate_objects_linearly(point_pair_a, point_pair_b)\n",
    "draw(finished_scene, scale=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Planes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plane_a = (up(0)^up(e1)^up(e2)^einf).normal()\n",
    "plane_b = (combined_rotor*plane_a*~combined_rotor).normal()\n",
    "intermediary_list, finished_scene = interpolate_objects_linearly(plane_a, plane_b)\n",
    "draw(finished_scene)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Spheres"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sphere_a = (up(0)^up(e1)^up(e2)^up(e3)).normal()\n",
    "sphere_b = (combined_rotor*sphere_a*~combined_rotor).normal()\n",
    "intermediary_list, finished_scene = interpolate_objects_linearly(sphere_a, sphere_b)\n",
    "draw(finished_scene, scale=0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
